home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.7 / tcpdump- / tcpdump-richard-1.7 / libpcap-0.0 / pcap-dlpi.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-12  |  11.2 KB  |  509 lines

  1. /*
  2.  * Copyright (c) 1993, 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * This code contributed by Atanu Ghosh (atanu@cs.ucl.ac.uk),
  22.  * University College London.
  23.  */
  24. #ifndef lint
  25. static  char rcsid[] =
  26.     "@(#)$Header: pcap-dlpi.c,v 1.22x 94/10/12 20:08:15 leres Exp $ (LBL)";
  27. #endif
  28.  
  29. /*
  30.  * Packet capture routine for dlpi under SunOS 5
  31.  *
  32.  * Notes:
  33.  *
  34.  *    - Apparently the DLIOCRAW ioctl() is specific to SunOS.
  35.  *
  36.  *    - There is a bug in bufmod(7) such that setting the snapshot
  37.  *      length results in data being left of the front of the packet.
  38.  *
  39.  *    - It might be desirable to use pfmod(7) to filter packets in the
  40.  *      kernel.
  41.  */
  42.  
  43. #include <sys/types.h>
  44. #include <sys/time.h>
  45. #include <sys/bufmod.h>
  46. #include <sys/dlpi.h>
  47. #include <sys/stream.h>
  48. #include <sys/systeminfo.h>
  49.  
  50. #include <net/bpf.h>
  51.  
  52. #include <ctype.h>
  53. #include <errno.h>
  54. #include <fcntl.h>
  55. #include <memory.h>
  56. #include <signal.h>
  57. #include <stdio.h>
  58. #if __STDC__
  59. #include <stdlib.h>
  60. #endif
  61. #include <string.h>
  62. #include <stropts.h>
  63. #include <unistd.h>
  64.  
  65. #include "pcap-int.h"
  66.  
  67. #define    MAXDLBUF    8192
  68.  
  69. /* Forwards */
  70. static int send_request(int, char *, int, char *, char *);
  71. static int dlattachreq(int, u_long, char *);
  72. static int dlinfoack(int, char *, char *);
  73. static int dlinforeq(int, char *);
  74. static int dlpromisconreq(int, u_long, char *);
  75. static int dlokack(int, char *, char *);
  76. static int strioctl(int, int, int, char *);
  77. #ifdef SOLARIS
  78. static char *getrelease(long *, long *, long *);
  79. #endif
  80.  
  81. int
  82. pcap_stats_live(pcap_t *p, struct pcap_stat *ps)
  83. {
  84.     *ps = p->md.stat;
  85.     return (0);
  86. }
  87.  
  88. int
  89. pcap_read_live(pcap_t *p, int cnt, pcap_handler callback, u_char *user)
  90. {
  91.     register int cc, n;
  92.     register u_char *bp, *ep, *pk;
  93.     register struct bpf_insn *fcode;
  94.     register struct sb_hdr *sbp;
  95.     int flags;
  96.     struct strbuf data;
  97.     struct pcap_hdr hdr;
  98.  
  99.     flags = 0;
  100.     cc = p->cc;
  101.     if (cc == 0) {
  102.         data.buf = (char *)p->buffer;
  103.         data.maxlen = MAXDLBUF;
  104.         data.len = 0;
  105.         do {
  106.             if (getmsg(p->fd, NULL, &data, &flags) < 0) {
  107.                 /* Don't choke when we get ptraced */
  108.                 if (errno == EINTR) {
  109.                     cc = 0;
  110.                     continue;
  111.                 }
  112.                 strcpy(p->errbuf, pcap_strerror(errno));
  113.                 return (-1);
  114.             }
  115.             cc = data.len;
  116.         } while (cc == 0);
  117.         bp = p->buffer;
  118.     } else
  119.         bp = p->bp;
  120.  
  121.     /* Loop through packets */
  122.     fcode = p->fcode.bf_insns;
  123.     ep = bp + cc;
  124.     n = 0;
  125.     while (bp < ep) {
  126.         sbp = (struct sb_hdr *)bp;
  127.         p->md.stat.ps_drop += sbp->sbh_drops;
  128.         ++p->md.stat.ps_recv;
  129.         pk = bp + sizeof(*sbp);
  130.         bp += sbp->sbh_totlen;
  131.         if (fcode == NULL ||
  132.             bpf_filter(fcode, pk, sbp->sbh_origlen, sbp->sbh_msglen)) {
  133.             hdr.ts = sbp->sbh_timestamp;
  134.             hdr.len = sbp->sbh_origlen;
  135.             hdr.caplen = sbp->sbh_msglen;
  136.             hdr.drops = p->md.stat.ps_drop;
  137.             (*callback)(user, &hdr, pk);
  138.             if (++n >= cnt && cnt >= 0) {
  139.                 p->cc = ep - bp;
  140.                 p->bp = bp;
  141.                 return (n);
  142.             }
  143.         }
  144.     }
  145.     p->cc = 0;
  146.     return (n);
  147. }
  148.  
  149. pcap_t *
  150. pcap_open_live(char *device, int snaplen, int promisc, int to_ms, char *ebuf)
  151. {
  152.     register pcap_t *p;
  153.     long    buf[MAXDLBUF];
  154.     int ppa;
  155.     int cppa;
  156.     register dl_info_ack_t *infop;
  157.     u_long ss, flag;
  158. #ifdef SOLARIS
  159.     char *release;
  160.     long osmajor, osminor, osmicro;
  161. #endif
  162.     char dname[100];
  163.  
  164.     p = (pcap_t *)malloc(sizeof(*p));
  165.     if (p == NULL) {
  166.         strcpy(ebuf, pcap_strerror(errno));
  167.         return (NULL);
  168.     }
  169.     memset(p, 0, sizeof(*p));
  170.  
  171.     /*
  172.     ** 1) In order to get the ppa take the last character of the device
  173.     ** name if it is a number then fail the open.
  174.     **
  175.     ** 2) If the name starts with a '/' then this is an absolute pathname,
  176.     ** otherwise prepend '/dev/'.
  177.     **
  178.     ** 3) Remove the trailing digit and try and open the device
  179.     ** not staggeringly intuitive but it should work.
  180.     **
  181.     ** If there are more than 9 devices this code will fail.
  182.     */
  183.  
  184.     cppa = device[strlen(device) - 1];
  185.     if (!isdigit(cppa)) {
  186.         sprintf(ebuf, "%c is not a digit, therefore not a valid ppa",
  187.             cppa);
  188.         goto bad;
  189.     }
  190.  
  191.     dname[0] = '\0';
  192.     if (device[0] != '/')
  193.         strcpy(dname, "/dev/");
  194.  
  195.     strcat(dname, device);
  196.     dname[strlen(dname) - 1] = '\0';
  197.  
  198.     if ((p->fd = open(dname, O_RDWR)) < 0) {
  199.         sprintf(ebuf, "%s: %s", dname, pcap_strerror(errno));
  200.         goto bad;
  201.     }
  202.     p->snapshot = snaplen;
  203.  
  204.     ppa = cppa - '0';
  205.     /*
  206.     ** Attach.
  207.     */
  208.     if (dlattachreq(p->fd, ppa, ebuf) < 0 ||
  209.         dlokack(p->fd, (char *)buf, ebuf) < 0)
  210.         goto bad;
  211.  
  212.     if (promisc) {
  213.         /*
  214.         ** enable promiscuous.
  215.         */
  216.         if (dlpromisconreq(p->fd, DL_PROMISC_PHYS, ebuf) < 0 ||
  217.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  218.             goto bad;
  219.         if (dlpromisconreq(p->fd, DL_PROMISC_SAP, ebuf) < 0 ||
  220.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  221.             goto bad;
  222.  
  223.         /*
  224.         ** enable multicast, you would have thought promiscuous
  225.         ** would be sufficient.
  226.         */
  227.         if (dlpromisconreq(p->fd, DL_PROMISC_MULTI, ebuf) < 0 ||
  228.             dlokack(p->fd, (char *)buf, ebuf) < 0)
  229.             goto bad;
  230.     }
  231.  
  232.     /*
  233.     ** Determine link type
  234.     */
  235.     if (dlinforeq(p->fd, ebuf) < 0 ||
  236.         dlinfoack(p->fd, (char *)buf, ebuf) < 0)
  237.         goto bad;
  238.  
  239.     infop = &((union DL_primitives *)buf)->info_ack;
  240.     switch (infop->dl_mac_type) {
  241.  
  242.     case DL_ETHER:
  243.         p->linktype = DLT_EN10MB;
  244.         break;
  245.  
  246.     case DL_FDDI:
  247.         p->linktype = DLT_FDDI;
  248.         break;
  249.  
  250.     default:
  251.         sprintf(ebuf, "unknown mac type 0x%lu", infop->dl_mac_type);
  252.         goto bad;
  253.     }
  254.  
  255. #ifdef    DLIOCRAW
  256.     /*
  257.     ** This is a non standard SunOS hack to get the ethernet header.
  258.     */
  259.     if (strioctl(p->fd, DLIOCRAW, 0, NULL) < 0) {
  260.         sprintf(ebuf, "DLIOCRAW: %s", pcap_strerror(errno));
  261.         goto bad;
  262.     }
  263. #endif
  264.  
  265.     /*
  266.     ** Another non standard call to get the data nicely buffered
  267.     */
  268.     if (ioctl(p->fd, I_PUSH, "bufmod") != 0) {
  269.         sprintf(ebuf, "I_PUSH bufmod: %s", pcap_strerror(errno));
  270.         goto bad;
  271.     }
  272.  
  273.     /*
  274.     ** Now that the bufmod is pushed lets configure it.
  275.     **
  276.     ** There is a bug in bufmod(7). When dealing with messages of
  277.     ** less than snaplen size it strips data from the beginning not
  278.     ** the end.
  279.     **
  280.     ** This bug is supposed to be fixed in 5.3.2. Also, there is a
  281.     ** patch available. Ask for bugid 1149065.
  282.     */
  283.     ss = snaplen;
  284. #ifdef SOLARIS
  285.     release = getrelease(&osmajor, &osminor, &osmicro);
  286.     if (osmajor == 5 && (osminor <= 2 || (osminor == 3 && osmicro < 2)) &&
  287.         getenv("BUFMOD_FIXED") == NULL) {
  288.         fprintf(stderr,
  289.         "WARNING: bufmod is broken in SunOS %s; ignoring snaplen.\n",
  290.             release);
  291.         ss = 0;
  292.     }
  293. #endif
  294.     if (ss > 0 &&
  295.         strioctl(p->fd, SBIOCSSNAP, sizeof(u_long), (char *)&ss) != 0) {
  296.         sprintf(ebuf, "SBIOCSSNAP: %s", pcap_strerror(errno));
  297.         goto bad;
  298.     }
  299.  
  300.     /*
  301.     ** Set up the bufmod flags
  302.     */
  303.     if (strioctl(p->fd, SBIOCGFLAGS, sizeof(u_long), (char *)&flag) < 0) {
  304.         sprintf(ebuf, "SBIOCGFLAGS: %s", pcap_strerror(errno));
  305.         goto bad;
  306.     }
  307.     flag |= SB_NO_DROPS;
  308.     if (strioctl(p->fd, SBIOCSFLAGS, sizeof(u_long), (char *)&flag) != 0) {
  309.         sprintf(ebuf, "SBIOCSFLAGS: %s", pcap_strerror(errno));
  310.         goto bad;
  311.     }
  312.     /*
  313.     ** Set up the bufmod timeout
  314.     */
  315.     if (to_ms != 0) {
  316.         struct timeval to;
  317.  
  318.         to.tv_sec = to_ms / 1000;
  319.         to.tv_usec = (to_ms * 1000) % 1000000;
  320.         if (strioctl(p->fd, SBIOCSTIME, sizeof(to), (char *)&to) != 0) {
  321.             sprintf(ebuf, "SBIOCSTIME: %s", pcap_strerror(errno));
  322.             goto bad;
  323.         }
  324.     }
  325.  
  326.     /*
  327.     ** As the last operation flush the read side.
  328.     */
  329.     if (ioctl(p->fd, I_FLUSH, FLUSHR) != 0) {
  330.         sprintf(ebuf, "FLUSHR: %s", pcap_strerror(errno));
  331.         goto bad;
  332.     }
  333.     /* Allocate data buffer */
  334.     p->bufsize = MAXDLBUF * sizeof(long);
  335.     p->buffer = (u_char *)malloc(p->bufsize);
  336.     return (p);
  337. bad:
  338.     free(p);
  339.     return (NULL);
  340. }
  341.  
  342. int
  343. pcap_setfilter_live(pcap_t *p, struct bpf_program *fp)
  344. {
  345.     p->fcode = *fp;
  346.     return (0);
  347. }
  348.  
  349. static int
  350. send_request(int fd, char *ptr, int len, char *what, char *ebuf)
  351. {
  352.     struct    strbuf    ctl;
  353.     int    flags;
  354.  
  355.     ctl.maxlen = 0;
  356.     ctl.len = len;
  357.     ctl.buf = ptr;
  358.  
  359.     flags = 0;
  360.     if (putmsg(fd, &ctl, (struct strbuf *) NULL, flags) < 0) {
  361.         sprintf(ebuf, "putmsg \"%s\"failed: %s",
  362.             what, pcap_strerror(errno));
  363.         return (-1);
  364.     }
  365.     return (0);
  366. }
  367.  
  368. static int
  369. dlattachreq(int fd, u_long ppa, char *ebuf)
  370. {
  371.     dl_attach_req_t    req;
  372.  
  373.     req.dl_primitive = DL_ATTACH_REQ;
  374.     req.dl_ppa = ppa;
  375.  
  376.     return (send_request(fd, (char *)&req, sizeof(req), "attach", ebuf));
  377. }
  378.  
  379. static int
  380. dlpromisconreq(int fd, u_long level, char *ebuf)
  381. {
  382.     dl_promiscon_req_t req;
  383.  
  384.     req.dl_primitive = DL_PROMISCON_REQ;
  385.     req.dl_level = level;
  386.  
  387.     return (send_request(fd, (char *)&req, sizeof(req), "promiscon", ebuf));
  388. }
  389.  
  390. static int
  391. dlokack(int fd, char *bufp, char *ebuf)
  392. {
  393.     union    DL_primitives    *dlp;
  394.     struct    strbuf    ctl;
  395.     int    flags;
  396.  
  397.     ctl.maxlen = MAXDLBUF;
  398.     ctl.len = 0;
  399.     ctl.buf = bufp;
  400.  
  401.     flags = 0;
  402.     if (getmsg(fd, &ctl, (struct strbuf*)NULL, &flags) < 0) {
  403.         sprintf(ebuf, "getmsg: %s", pcap_strerror(errno));
  404.         return (-1);
  405.     }
  406.  
  407.     dlp = (union DL_primitives *) ctl.buf;
  408.  
  409.     if (dlp->dl_primitive != DL_OK_ACK) {
  410.         sprintf(ebuf, "dlokack unexpected primitive %d",
  411.             dlp->dl_primitive);
  412.         return (-1);
  413.     }
  414.  
  415.     if (ctl.len != sizeof(dl_ok_ack_t)) {
  416.         sprintf(ebuf, "dlokack incorrect size returned");
  417.         return (-1);
  418.     }
  419.     return (0);
  420. }
  421.  
  422.  
  423. static int
  424. dlinforeq(int fd, char *ebuf)
  425. {
  426.     dl_info_req_t req;
  427.  
  428.     req.dl_primitive = DL_INFO_REQ;
  429.  
  430.     return (send_request(fd, (char *)&req, sizeof(req), "info", ebuf));
  431. }
  432.  
  433. static int
  434. dlinfoack(int fd, char *bufp, char *ebuf)
  435. {
  436.     union    DL_primitives    *dlp;
  437.     struct    strbuf    ctl;
  438.     int    flags;
  439.  
  440.     ctl.maxlen = MAXDLBUF;
  441.     ctl.len = 0;
  442.     ctl.buf = bufp;
  443.  
  444.     flags = 0;
  445.     if (getmsg(fd, &ctl, (struct strbuf *)NULL, &flags) < 0) {
  446.         sprintf(ebuf, "dlinfoack: getmsg: %s", pcap_strerror(errno));
  447.         return (-1);
  448.     }
  449.  
  450.     dlp = (union DL_primitives *) ctl.buf;
  451.  
  452.     if (dlp->dl_primitive != DL_INFO_ACK) {
  453.         sprintf(ebuf, "dlinfoack: unexpected primitive %ld",
  454.             dlp->dl_primitive);
  455.         return (-1);
  456.     }
  457.  
  458.     /* Extra stuff like the broadcast address can be returned */
  459.     if (ctl.len < DL_INFO_ACK_SIZE) {
  460.         sprintf(ebuf, "dlinfoack: incorrect size returned");
  461.         return (-1);
  462.     }
  463.     return (0);
  464. }
  465.  
  466. static int
  467. strioctl(int fd, int cmd, int len, char *dp)
  468. {
  469.     struct strioctl str;
  470.     int rc;
  471.  
  472.     str.ic_cmd = cmd;
  473.     str.ic_timout = -1;
  474.     str.ic_len = len;
  475.     str.ic_dp = dp;
  476.     rc = ioctl(fd, I_STR, &str);
  477.  
  478.     if (rc < 0)
  479.         return (rc);
  480.     else
  481.         return (str.ic_len);
  482. }
  483.  
  484. #ifdef SOLARIS
  485. static char *
  486. getrelease(long *majorp, long *minorp, long *microp)
  487. {
  488.     char *cp;
  489.     static char buf[32];
  490.  
  491.     *majorp = 0;
  492.     *minorp = 0;
  493.     *microp = 0;
  494.     if (sysinfo(SI_RELEASE, buf, sizeof(buf)) < 0)
  495.         return ("?");
  496.     cp = buf;
  497.     if (!isdigit(*cp))
  498.         return (buf);
  499.     *majorp = strtol(cp, &cp, 10);
  500.     if (*cp++ != '.')
  501.         return (buf);
  502.     *minorp =  strtol(cp, &cp, 10);
  503.     if (*cp++ != '.')
  504.         return (buf);
  505.     *microp =  strtol(cp, &cp, 10);
  506.     return (buf);
  507. }
  508. #endif
  509.